#include "MainView.hpp"
#include "TreeView.hpp"
#include "ObjectView.hpp"
#include "HelpView.hpp"
#include "FileDialogView.hpp"
#include "MenuBar.hpp"
#include "StatusBar.hpp"
#include "IconBar.hpp"
#include "TimelineBar.hpp"
#include "GlView.hpp"
#include "M3gView.hpp"
#include "CameraController.hpp"
#include "Scene.hpp"
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Menu_Bar.H>
#include <iostream>
#include "constants.hpp"
using namespace m3g_viewer;
using namespace std;


static
void callback_quit (Fl_Window* w, void* v)
{
    MainView* view = (MainView*)v;
    view->quit ();
}



MainView:: MainView () : Fl_Window(MAIN_VIEW_WIDTH,
                                   MAIN_VIEW_HEIGHT),
                         m3g_view(0), object_view(0), tree_view(0), help_view(0)
{

    MenuBar* menu = new MenuBar (this);

    IconBar* icons = new IconBar (this);

    m3g_view = new M3gView (this);
    resizable (m3g_view);

    TimelineBar* timeline = new TimelineBar (this);

    StatusBar* status = new StatusBar (this);

    end ();
}
 
MainView:: ~MainView ()
{
    if (object_view) {
        delete object_view;
    }
    if (tree_view) {
        delete tree_view;
    }
    if (help_view) {
        delete help_view;
    }
}




void MainView:: quit ()
{
    if (object_view) {
        object_view->hide ();
    }
    if (tree_view) {
        tree_view->hide ();
    }
    if (help_view) {
        help_view->hide ();
    }

    hide ();
}

void MainView:: open_file_dialog ()
{
    Fl_File_Chooser::show_label      = "拡張子：";
    Fl_File_Chooser::favorites_label = "お気に入り";
    Fl_File_Chooser::filename_label  = "ファイル名：";

    FileDialogView* view = new FileDialogView ();
    view->show ();
/*
    int ret = view->show ();
    switch (ret) {
    case -1: // Error
    case  1: // Cancel
    defualt: // OK.
        cout << "Open : " << view->filename() << "\n";
    };
*/
    // view->getFileName
    // ここでM3Gをロードして
    // シーンを表示
}

/**
 * (*1) Xだとウィンドウ枠が含まれない値が帰ってくる
 */
void MainView:: show_tree_view ()
{
    if (tree_view == 0) {
        int width = decorated_w();
        if (width == w()) {  // (*1)
            width += 10;
        }
        int x = x_root() + width;
        int y = y_root();
        tree_view = new TreeView (this, x, y);
    }
    tree_view->show ();
}

void MainView:: show_object_view (void* data)
{
    if (object_view == 0) {
        int width = decorated_w();
        if (width == w()) {  // (*1)
            width += 10;
        }
        int height = decorated_h();
        if (tree_view) {
            height += tree_view->decorated_h();
        }
        int x = x_root() + width;
        int y = y_root() + height;
        object_view = new ObjectView (data);
    }
    object_view->show ();
    object_view->take_focus ();
}


void MainView:: show_help_view ()
{
    if (help_view == 0) {
        help_view = new HelpView ();
    }
    help_view->show ();
}

M3gView* MainView:: get_m3g_view () const
{
    return m3g_view;
}
